package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.UserPostFavorite;
import com.scuec.groupfive.mapper.UserPostFavoriteMapper;
import com.scuec.groupfive.service.UserPostFavoriteService;
import com.scuec.groupfive.vo.UserPostFavoriteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserPostFavoriteServiceImpl implements UserPostFavoriteService {

    @Autowired
    private UserPostFavoriteMapper favoriteMapper;

    @Override
    @Transactional
    public String favoritePost(UserPostFavoriteVO postFavoriteVO) {
        Integer isFavorite = favoriteMapper.checkPostFavoriteStatus(postFavoriteVO.getPost_id());
        if (isFavorite != null && isFavorite == 1) {
            UserPostFavorite favorite = new UserPostFavorite();
            favorite.setPost_id(postFavoriteVO.getPost_id());
            favorite.setUser_id(postFavoriteVO.getUser_id());
            favoriteMapper.insertFavorite(favorite);
            return "收藏成功";
        }
        return "帖子不可收藏或不存在";
    }

    @Override
    @Transactional
    public String unfavoritePost(UserPostFavoriteVO postFavoriteVO) {
        favoriteMapper.updateStatusToInactive(
                postFavoriteVO.getPost_id(),
                postFavoriteVO.getUser_id()
        );
        return "取消收藏成功";
    }
}